package flex.cc.basicinformation.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import flex.cc.base.dto.LoginAuthDto;
import flex.cc.basicinformation.entity.ChannelEntity;
import flex.cc.basicinformation.service.ChannelService;
import flex.cc.basicinformation.vo.ChannelVo;
import flex.cc.core.annotation.LogAnnotation;
import flex.cc.core.annotation.ValidateAnnotation;
import flex.cc.core.support.BaseController;
import flex.cc.wrapper.WrapMapper;
import flex.cc.wrapper.Wrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 权限表
 *
 * @author guozejun
 * @email guozejun@sinosoft.com.cn
 * @date 2021-10-14 22:29:37
 */
@RestController
@RequestMapping(value = "/basic/information")
public class ChannelController extends BaseController {

    @Resource
    private ChannelService channeService;

    /**
     * 分页查询渠道信息.
     */
    @PostMapping(value = "/queryChannel")
    @ApiOperation(httpMethod = "POST", value = "查询渠道商基本信息")
    public Wrapper<IPage<ChannelEntity>> queryChannelPage(@ApiParam(name = "channe", value = "渠道信息") @RequestBody ChannelEntity channe) {
        logger.info("查询渠道商基本信息queryChannel={}", channe);
        IPage<ChannelEntity> page = channeService.queryChannelPage(channe);
        return WrapMapper.ok(page);
    }

    /**
     * 保存渠道信息
     */
    @PostMapping(value = "/saveChannel")
    @ApiOperation(httpMethod = "POST", value = "新增渠道信息")
    @ValidateAnnotation
    @LogAnnotation
    public Wrapper saveChannel(@ApiParam(name = "action", value = "渠道信息") @RequestBody ChannelEntity channe) {
        LoginAuthDto loginAuthDto = this.getLoginAuthDto();
        channeService.saveChannel(channe, loginAuthDto);
        return WrapMapper.ok();
    }

    @GetMapping(value = "/getChannelName/{channelName}/{id}")
    @ApiOperation(httpMethod = "GET", value = "查询渠道商名称是否重复")
    public Wrapper<String> getChannelName(@PathVariable(value = "channelName") String channelName, @PathVariable(value = "id") String id) {
    	String result=channeService.getChannelName(channelName, id);
    	return WrapMapper.ok(result);
    }

    @PostMapping(value = "/getAllChannelName")
    @ApiOperation(httpMethod = "POST", value = "查询服务商信息")
    public Wrapper<List<ChannelEntity>> getAllChannelName() {
        List<ChannelEntity> list = channeService.getAllChannelName();
        return WrapMapper.ok(list);
    }

    @GetMapping(value = "/getChannelCode/{channelCode}/{id}")
    @ApiOperation(httpMethod = "GET", value = "查询渠道商code是否重复")
    public Wrapper<String> getChannelCode(@PathVariable(value = "channelCode") String channelCode, @PathVariable(value = "id") String id) {
    	String result=channeService.getChannelCode(channelCode, id);
    	return WrapMapper.ok(result);
    }
    @LogAnnotation
    @PostMapping(value = "/getChannelDetail")
    @ApiOperation(httpMethod = "POST", value = "转办跳转工单处理页面")
	public Wrapper transferDetail(@RequestBody ChannelEntity channel) {
    	ChannelEntity vo1=channeService.getChannelDetail(channel.getId());
    	return WrapMapper.ok(vo1);
	}

    /**
     * @description 启用渠道商【2.2-服务包配置需求】
     * @param id 渠道商主键
     * @param status 目标状态
     * @return
     * @author wkangle
     * @time 2022/02/23 0023 18:08
     * @exception/throws
     */
    @LogAnnotation
    @GetMapping(value = "/channel/switchStatus/{id}/{status}")
    @ApiOperation(httpMethod = "GEt", value = "渠道状态修改")
    public Wrapper<Map<String, String>> switchStatus(@ApiParam(name = "reqVo", value = "请求信息") @PathVariable(value = "id") Long id, @PathVariable(value = "status") String status) {
        Map<String, String> switchStatus = channeService.switchStatus(id, status);
        return WrapMapper.ok(switchStatus);
    }

    /**
     * @description 手工单页面任务来源-仅允许选择启用状态的渠道商【2.2-服务包配置需求】
     * @return
     * @author wkangle
     * @time 2022-02-25 09:35:26
     * @exception/throws
     */
    @GetMapping(value = "/choseChannel")
    @ApiOperation(httpMethod = "GET", value = "手工单页面任务来源-仅允许选择启用状态的渠道商")
    public Wrapper<List<ChannelEntity>> choseChannel() {
        Map<String, String> channelMap = new HashMap<>();
        channelMap.put("status", "1");
        List<ChannelEntity> channelList = channeService.findChannelByParam(channelMap);
        return WrapMapper.ok(channelList);
    }

    /**
     * @description 根据渠道商主键id查询一个，用于修改回显【2.2-服务包配置需求】
     * @param id 渠道商主键id
     * @return
     * @author wkangle
     * @time 2022/03/04 0004 8:54
     * @exception/throws
    */
    @GetMapping(value = "/channel/queryOne/{id}")
    @ApiOperation(httpMethod = "GET", value = "查询一个用于修改")
    public Wrapper<ChannelVo> queryOne(@ApiParam(name = "action", value = "渠道商id") @PathVariable(value = "id") Long id) {
        logger.info("{} /channel/queryOne ==> {}", new Date(), id);
        ChannelVo vo = channeService.queryOne(id);
        return WrapMapper.ok(vo);
    }

    /**
    *
    * @author WANGCONG
    * 2022年4月14日上午10:43:52
    * @desc 渠道商clientId校验唯一性
    */
    @GetMapping(value = "/getChannelClientId/{clientId}/{id}")
    @ApiOperation(httpMethod = "GET", value = "查询渠道商clientId是否存在")
    public Wrapper<String> getChannelClientId(@PathVariable(value = "clientId") String clientId, @PathVariable(value = "id") String id) {
    	String result=channeService.getChannelClientId(clientId, id);
    	return WrapMapper.ok(result);
    }
    
}
